//
//  HomeStrategyViewController.m
//  GiftNote
//
//  Created by lanou on 15/8/15.
//  Copyright (c) 2015年 GN. All rights reserved.
//

#define kStrategy @"http://api.liwushuo.com/v2/brands/editor?limit=20&offset="

#import "HomeStrategyViewController.h"
#import "HomeHeader.h"
#import "HomeStrategyCell.h"
#import "LORequestManger.h"
#import "HomeStrategyModel.h"
#import "HomeStrategyHeadView.h"
#import "HomeStrategyLeftCell.h"
#import "HomeStrategyRightCell.h"
#import "HomeStrategyDetailController.h"
#import "MJRefresh.h"

#define kLeftGap 10
#define kGap 5

typedef NS_ENUM(NSInteger, RefreshDownLoadType) {
    RefreshDownLoadTypeUp,
    RefreshDownLoadTypeDown
};


@interface HomeStrategyViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic, assign) RefreshDownLoadType refreshDownLoadType;

@end

@implementation HomeStrategyViewController

- (NSMutableArray *)dataSource
{
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"品牌专区";
    //  改变标题的颜色
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                [UIColor whiteColor],
                                NSForegroundColorAttributeName, nil];
    [self.navigationController.navigationBar setTitleTextAttributes:attributes];
    //  改变返回按钮的颜色
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    
    _num = 0;
    _refreshDownLoadType = RefreshDownLoadTypeUp;
    [self creatTableView];
    
    [self readData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)creatTableView
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kHomeWidth, kHomeHeight) style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorColor = [UIColor whiteColor];
    _tableView.rowHeight = (kHomeWidth - kLeftGap * 2 - kGap * 2) / 2 + 10;
    _tableView.sectionHeaderHeight = 50;
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[HomeStrategyLeftCell class] forCellReuseIdentifier:@"HomeStrategyLeftCell"];
    [_tableView registerClass:[HomeStrategyRightCell class] forCellReuseIdentifier:@"HomeStrategyRightCell"];
    
    [_tableView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(headRefreshAction)];
    [_tableView addGifFooterWithRefreshingTarget:self refreshingAction:@selector(footRefreshAction)];
}

- (void)readData
{
    NSString *url = [kStrategy stringByAppendingString:[NSString stringWithFormat:@"%ld",(long)_num]];
    [LORequestManger GET:url success:^(id response) {
        
        if (_refreshDownLoadType == RefreshDownLoadTypeUp) {
            [self.dataSource removeAllObjects];
        }
        
        NSDictionary *dic = (NSDictionary *)response;
        NSArray *array = dic[@"data"][@"brands"];
        for (NSDictionary *newDic in array) {
            HomeStrategyModel *stategyModel = [[HomeStrategyModel alloc]init];
            [stategyModel setValuesForKeysWithDictionary:newDic];
            [self.dataSource addObject:stategyModel];
        }
        
        [_tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}


#pragma mark - UITableViewDelegate / UITabelViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeStrategyModel *stratrgyModel = _dataSource[indexPath.section];
    if (indexPath.section % 2 == 1) {
        HomeStrategyLeftCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeStrategyLeftCell" forIndexPath:indexPath];
        cell.array = stratrgyModel.image_urls;
        return cell;
    } else {
        HomeStrategyRightCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeStrategyRightCell"];
        cell.array = stratrgyModel.image_urls;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeStrategyDetailController *detailVC = [[HomeStrategyDetailController alloc]init];
    HomeStrategyModel *model = _dataSource[indexPath.section];
    detailVC.ID = [NSString stringWithFormat:@"%d",[model.ID intValue]];
    [self.navigationController pushViewController:detailVC animated:YES];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HomeStrategyHeadView *headView = [[HomeStrategyHeadView alloc]init];
    
    headView.strategyModel = _dataSource[section];
    return headView;
}

//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
//{
//    HomeStrategyHeadView *headView = [[HomeStrategyHeadView alloc]init];
//    headView.strategyModel = _dataSource[section];
//    return headView;
//}

- (void)headRefreshAction
{
    _num = 0;
    _refreshDownLoadType = RefreshDownLoadTypeUp;
    [self readData];
    [_tableView.header endRefreshing];
}

- (void)footRefreshAction
{
    _num += 20;
    _refreshDownLoadType = RefreshDownLoadTypeDown;
    [self readData];
    [_tableView.footer endRefreshing];
}


@end
